package com.bhajanswarupa.paginationutils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * Represents an order imposed upon a result set
 */
public class Order implements Serializable {

	private final boolean ascending;
	private boolean ignoreCase;
	private final String propertyName;

    private static final Logger LOGGER = LoggerFactory.getLogger(Order.class);
	private static final long serialVersionUID = -296236270268797922L;


	@Override
	public String toString() {

		return propertyName + ' ' + (ascending ? "ASC" : "DESC");
	}


	public Order ignoreCase() {

		ignoreCase = true;
		return this;
	}


	/**
	 * Constructor for Order.
	 */
	protected Order(String propertyName, boolean ascending) {

		this.propertyName = propertyName;
		this.ascending = ascending;
	}


	/**
	 * Ascending order
	 * 
	 * @param propertyName
	 * @return Order
	 */
	public static Order asc(String propertyName) {

		return new Order(propertyName, true);
	}


	/**
	 * Descending order
	 * 
	 * @param propertyName
	 * @return Order
	 */
	public static Order desc(String propertyName) {

		return new Order(propertyName, false);
	}


	public org.hibernate.criterion.Order toHibernateOrder() {

		if (ascending) {
			return org.hibernate.criterion.Order.asc(propertyName);
		} else {
			return org.hibernate.criterion.Order.desc(propertyName);
		}
	}
}
